package me.eleme.anubis.openapi.api.service;

import com.alibaba.fastjson.JSON;
import me.eleme.anubis.openapi.api.annotation.Service;
import me.eleme.anubis.openapi.api.base.BaseBizApiService;
import me.eleme.anubis.openapi.api.entity.order.*;
import me.eleme.anubis.openapi.api.exception.ServiceException;
import me.eleme.anubis.openapi.config.Config;
import me.eleme.anubis.openapi.config.oauth.response.Token;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuzhiyong
 */
@Service("me.ele.order")
public class OrderService extends BaseBizApiService {

    /**
     * 查询配送范围接口
     */
    private static final String CHAINSTORE_RANGE_API_CODE = "chainstoreRange";

    /**
     * 预下单apiCode
     */
    private static final String PRE_CREATE_ORDER_API_CODE = "preCreateOrder";
    /**
     * 创建订单apiCode
     */
    private static final String CREATE_ORDER_API_CODE = "createOrder";

    /**
     * 查询余额apiCode
     */
    private static final String GET_AMOUNT_API_CODE = "getAmount";
    /**
     * 查询订单详情apiCode
     */
    private static final String ORDER_DETAIL_API_CODE = "getOrderDetail";

    /**
     * 查询骑手信息apiCode
     */
    private static final String KNIGHT_INFO_API_CODE = "getKnightInfo";

    /**
     * 加小费apiCode
     */
    private static final String ADD_TIP_API_CODE = "addTip";

    /**
     * 获取可用取消原因列表apiCode
     */
    private static final String CANCEL_REASON_LIST_API_CODE = "getCancelReasonList";

    /**
     * 预取消订单apiCode
     */
    private static final String PRE_CANCEL_ORDER_API_CODE = "preCancelOrder";

    /**
     * 正式取消订单apiCode
     */
    private static final String CANCEL_ORDER_API_CODE = "cancelOrder";

    /**
     * 投诉订单apiCode
     */
    private static final String COMPLAINT_ORDER_API_CODE = "complaintOrder";

    /**
     * 索赔订单apiCode
     */
    private static final String CLAIM_ORDER_API_CODE = "claimOrder";

    public OrderService(Config config, Token token) {
        super(config, token, OrderService.class);
    }

    /**
     * 查询配送范围接口
     * @param chainstoreRangeReq 查询配送范围请求参数
     * @return 查询配送范围响应结果
     * @throws ServiceException 服务异常
     */
    public ChainstoreRangeRes chainstoreRange(ChainstoreRangeReq chainstoreRangeReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("chain_store_id",chainstoreRangeReq.getChainStoreId());
        params.put("out_shop_code",chainstoreRangeReq.getOutShopCode());
        params.put("basic_goods_id",chainstoreRangeReq.getBasicGoodsId());
        return call(CHAINSTORE_RANGE_API_CODE, params);
    }

    /**
     * 预下单接口
     * @param preCreateOrderReq 预下单请求参数
     * @return 预下单响应结果
     * @throws ServiceException 服务异常
     */
    public PreCreateOrderRes preCreateOrder(PreCreateOrderReq preCreateOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(32);
        params.put("partner_order_code",preCreateOrderReq.getPartnerOrderCode());
        params.put("out_shop_code",preCreateOrderReq.getOutShopCode());
        params.put("chain_store_id",preCreateOrderReq.getChainStoreId());
        params.put("order_source",preCreateOrderReq.getOrderSource());
        params.put("service_goods_id",preCreateOrderReq.getServiceGoodsId());
        params.put("base_goods_id",preCreateOrderReq.getBaseGoodsId());
        params.put("order_type",preCreateOrderReq.getOrderType());
        params.put("require_receive_time",preCreateOrderReq.getRequireReceiveTime());
        params.put("order_add_time",preCreateOrderReq.getOrderAddTime());
        params.put("use_coupon",preCreateOrderReq.getUseCoupon());
        params.put("expect_fetch_time",preCreateOrderReq.getExpectFetchTime());
        params.put("transport_address",preCreateOrderReq.getTransportAddress());
        params.put("position_source",preCreateOrderReq.getPositionSource());
        params.put("receiver_address",preCreateOrderReq.getReceiverAddress());
        params.put("receiver_longitude",preCreateOrderReq.getReceiverLongitude());
        params.put("receiver_latitude",preCreateOrderReq.getReceiverLatitude());
        params.put("goods_total_amount_cent",preCreateOrderReq.getGoodsTotalAmountCent());
        params.put("goods_actual_amount_cent",preCreateOrderReq.getGoodsActualAmountCent());
        params.put("order_tip_amount_cent",preCreateOrderReq.getOrderTipAmountCent());
        params.put("goods_weight",preCreateOrderReq.getGoodsWeight());
        params.put("goods_count",preCreateOrderReq.getGoodsCount());
        params.put("goods_item_list",preCreateOrderReq.getGoodsItemList());
        return call(PRE_CREATE_ORDER_API_CODE, params);
    }

    /**
     * @param createOrderReq 创建订单请求参数
     * @return 创建订单响应实体
     * @throws ServiceException 服务异常
     */
    public CreateOrderRes createOrder(CreateOrderReq createOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(32);
        params.put("partner_order_code", createOrderReq.getPartnerOrderCode());
        params.put("out_shop_code", createOrderReq.getOutShopCode());
        params.put("chain_store_id", createOrderReq.getChainStoreId());
        params.put("order_source", createOrderReq.getOrderSource());
        params.put("service_goods_id", createOrderReq.getServiceGoodsId());
        params.put("base_goods_id", createOrderReq.getBaseGoodsId());
        params.put("order_type", createOrderReq.getOrderType());
        params.put("require_receive_time", createOrderReq.getRequireReceiveTime());
        params.put("order_add_time", createOrderReq.getOrderAddTime());
        params.put("use_coupon", createOrderReq.getUseCoupon());
        params.put("expect_fetch_time", createOrderReq.getExpectFetchTime());
        params.put("transport_address", createOrderReq.getTransportAddress());
        params.put("position_source", createOrderReq.getPositionSource());
        params.put("receiver_address", createOrderReq.getReceiverAddress());
        params.put("receiver_longitude", createOrderReq.getReceiverLongitude());
        params.put("receiver_latitude", createOrderReq.getReceiverLatitude());
        params.put("goods_total_amount_cent", createOrderReq.getGoodsTotalAmountCent());
        params.put("goods_actual_amount_cent", createOrderReq.getGoodsActualAmountCent());
        params.put("order_tip_amount_cent", createOrderReq.getOrderTipAmountCent());
        params.put("goods_weight", createOrderReq.getGoodsWeight());
        params.put("goods_count", createOrderReq.getGoodsCount());
        params.put("goods_item_list", createOrderReq.getGoodsItemList());
        params.put("serial_number", createOrderReq.getSerialNumber());
        params.put("order_source_order_id", createOrderReq.getOrderSourceOrderId());
        params.put("receiver_name", createOrderReq.getReceiverName());
        params.put("receiver_primary_phone", createOrderReq.getReceiverPrimaryPhone());
        params.put("receiver_second_phone", createOrderReq.getReceiverSecondPhone());
        params.put("customer_ext_tel", createOrderReq.getCustomerExtTel());
        params.put("order_remark", createOrderReq.getOrderRemark());
        params.put("pre_create_order_t_index_id", createOrderReq.getPreCreateOrderTIndexId());
        params.put("warehouse_id", createOrderReq.getWarehouseId());
        params.put("actual_delivery_amount_cent", createOrderReq.getActualDeliveryAmountCent());
        params.put("write_off_code", createOrderReq.getWriteOffCode());
        params.put("cancel_code", createOrderReq.getCancelCode());
        params.put("fetch_code", createOrderReq.getFetchCode());
        return call(CREATE_ORDER_API_CODE, params);
    }



    /**
     * 查询余额
     * @return 查询余额响应实体
     * @throws ServiceException 服务异常
     */
    public GetAmountRes getAmount() throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        return call(GET_AMOUNT_API_CODE, params);
    }



    /**
     * 查询订单详情
     * @return 查询订单详情响应实体
     * @throws ServiceException 服务异常
     */
    public GetOrderDetailRes getOrderDetail(GetOrderDetailReq getOrderDetailReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",getOrderDetailReq.getOrderId());
        params.put("partner_order_code",getOrderDetailReq.getPartnerOrderCode());
        return call(ORDER_DETAIL_API_CODE, params);
    }


    /**
     * 查询骑手信息
     * @param knightInfoReq 查询骑手信息请求实体
     * @return 查询骑手信息响应实体
     * @throws ServiceException 服务异常
     */
    public GetKnightInfoRes getKnightInfo(GetKnightInfoReq knightInfoReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",knightInfoReq.getOrderId());
        params.put("partner_order_code",knightInfoReq.getPartnerOrderCode());
        return call(KNIGHT_INFO_API_CODE, params);
    }

    /**
     * 加小费接口
     * @return 加小费响应实体
     * @throws ServiceException 服务异常
     */
    public AddTipRes addTip(AddTipReq addTipReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",addTipReq.getOrderId());
        params.put("partner_order_code",addTipReq.getPartnerOrderCode());
        params.put("add_tip_amount_cent",addTipReq.getAddTipAmountCent());
        params.put("third_index_id",addTipReq.getThirdIndexId());
        return call(ADD_TIP_API_CODE, params);
    }


    /**
     *
     * 获取可用取消原因列表接口
     * @param cancelReasonListReq 获取可用取消原因列表请求实体
     * @return 获取可用取消原因列表响应实体
     * @throws ServiceException 服务异常
     */
    public CancelReasonListRes getCancelReasonList(CancelReasonListReq cancelReasonListReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",cancelReasonListReq.getOrderId());
        params.put("partner_order_code",cancelReasonListReq.getPartnerOrderCode());
        return call(CANCEL_REASON_LIST_API_CODE, params);
    }


    /**
     * 预取消订单接口
     * @param preCancelOrderReq 预取消订单请求实体
     * @return 预取消订单响应实体
     * @throws ServiceException 服务异常
     */
    public PreCancelOrderRes preCancelOrder(PreCancelOrderReq preCancelOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",preCancelOrderReq.getOrderId());
        params.put("partner_order_code",preCancelOrderReq.getPartnerOrderCode());
        params.put("order_cancel_code",preCancelOrderReq.getOrderCancelCode());
        return call(PRE_CANCEL_ORDER_API_CODE, params);
    }


    /**
     * 正式取消订单接口
     * @param cancelOrderReq 正式取消订单请求实体
     * @return 正式取消订单响应实体
     * @throws ServiceException 服务异常
     */
    public CancelOrderRes cancelOrder(CancelOrderReq cancelOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",cancelOrderReq.getOrderId());
        params.put("partner_order_code",cancelOrderReq.getPartnerOrderCode());
        params.put("order_cancel_code",cancelOrderReq.getOrderCancelCode());
        params.put("actual_cancel_cost_cent",cancelOrderReq.getActualCancelCostCent());
        params.put("order_cancel_other_reason",cancelOrderReq.getOrderCancelOtherReason());
        params.put("order_cancel_role",cancelOrderReq.getOrderCancelRole());
        params.put("need_reverse_order",cancelOrderReq.isNeedReverseOrder());
        return call(CANCEL_ORDER_API_CODE, params);
    }


    /**
     * 投诉订单接口
     * @param complaintOrderReq 投诉订单请求实体
     * @return 投诉订单响应实体
     * @throws ServiceException 服务异常
     */
    public ComplaintOrderRes complaintOrder(ComplaintOrderReq complaintOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",complaintOrderReq.getOrderId());
        params.put("partner_order_code",complaintOrderReq.getPartnerOrderCode());
        params.put("order_complaint_code",complaintOrderReq.getOrderComplaintCode());
        params.put("order_complaint_desc",complaintOrderReq.getOrderComplaintDesc());
        return call(COMPLAINT_ORDER_API_CODE, params);
    }


    /**
     * 索赔订单接口
     * @param claimOrderReq 索赔订单请求实体
     * @return 索赔订单响应实体
     * @throws ServiceException 服务异常
     */
    public ClaimOrderRes claimOrder(ClaimOrderReq claimOrderReq) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>(8);
        params.put("order_id",claimOrderReq.getOrderId());
        params.put("partner_order_code",claimOrderReq.getPartnerOrderCode());
        params.put("order_claim_code",claimOrderReq.getOrderClaimCode());
        params.put("order_claim_desc",claimOrderReq.getOrderClaimDesc());
        params.put("order_claim_price_cent",claimOrderReq.getOrderClaimPriceCent());
        params.put("order_claim_sk",JSON.toJSONString(claimOrderReq.getOrderClaimSku()));
        return call(CLAIM_ORDER_API_CODE, params);
    }



}
